package com.test.io.homeWork;

import com.test.io.buffer.obj.Dog;
import org.junit.Test;

import java.io.*;
import java.util.Properties;

/**
 * @version 1.0
 * @Author: 许啸林 io流作业练习
 * @Date: 2021/6/5 8:12
 */
public class WorkDemo {

    /**
     *     练习 1
     *     1. 判断文件 e盘下是否有文件夹mytemp,没有就创建
     *     2. 在e:\\mytemp 目录下,创建文件hello.text
     *     3. 如果hello.txt 已经存在,提示该文件已存在
     *     4. 向文件内追加写入信息
     */
    @Test
    public void createFile() {
        BufferedWriter fileWriter = null;
        try {
            String filepath = "hello.txt";
            File folderPath = new File("e:\\mytemp");
            File newFile = new File(folderPath, filepath);
            if(!folderPath.exists()) {
                folderPath.mkdirs();
                System.out.println("文件夹创建成功");
            } else {
                System.out.println("文件夹已存在");
            }
            Boolean fileExits = false;
            fileExits = newFile.exists();
            if(fileExits){
                System.out.println("文件已经存在");
            }else {
                newFile.createNewFile();
                System.out.println("文件创建成功");
            }
            fileWriter = new BufferedWriter(new FileWriter(newFile.getPath(), true));
            String writeStr = "翳翳桑榆日,照我征衣裳";
            if(fileExits) {
                fileWriter.newLine();
            }
            fileWriter.write(writeStr);
            System.out.println("向文件中追加: " + writeStr);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     *    练习 2
     *    1.使用BufferedReader 读取文本文件内容,
     *    2.转gbk码并在内容中追加行号, 连同内容一并输出到屏幕上
     */
    @Test
    public void remoteFile() {
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("e:\\mytemp\\hello.txt"), "gbk"));
            String readData = null;
            int index = 0;
            while ((readData = bufferedReader.readLine()) != null) {
                System.out.println(++index +". "+ readData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     *    练习 3
     *    1.编写 dog.properties name=tom age=5 color=red
     *    2.创建dog对象并从dog.properties上读取属性值
     *    3.将dog对象序列化到
     *    4.反序列化dog并输出到控制台
     */
    @Test
    public void readProperties() {
        Properties properties = new Properties();
        ObjectOutputStream ops = null;
        ObjectInputStream ois = null;
        try {
            properties.load(new FileReader("src//dog.properties"));
            String user = String.valueOf(properties.get("name"));
            Integer age = Integer.parseInt(String.valueOf(properties.get("age"))) ;
            String color = String.valueOf(properties.getProperty("color"));
            Dog.nation = "中国";
            Dog dog = new Dog(user, age, color);
            System.out.println("读取对象信息: " + dog.toString());
            // 序列化到 dog.text
            ops = new ObjectOutputStream(new FileOutputStream("src//dog.text"));
            ops.writeObject(dog);
            System.out.println("序列化到dog.text");
            // 反序列化获取对象并输出到控制台
            ois = new ObjectInputStream(new FileInputStream("src//dog.text"));
            Dog readDog = (Dog) ois.readObject();
            System.out.println("反序列化对象信息: " + readDog.toString() );
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ops != null) {
                    ops.close();
                }
                if(ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
